<?php 

//tim kiem
$flag=isset($_REQUEST['SO_tim' ])?$_REQUEST['SO_tim' ]:"";
$sea =isset($_REQUEST['SO_sea' ])?$_REQUEST['SO_sea' ]:"";


$sea2=isset($_REQUEST['SO_sea2'])?$_REQUEST['SO_sea2']:"";

$sea3=isset($_REQUEST['SO_sea3'])?$_REQUEST['SO_sea3']:"";

//end tim kiem
//sap xep
$kieu=isset($_REQUEST['SO_kieu'])?$_REQUEST['SO_kieu']:"DESC";
$cot =isset($_REQUEST['SO_cot' ])?$_REQUEST['SO_cot' ]:"id";
if($cot!='' && $cot=='id'){
$cot='`key`';
if(isset($_REQUEST['SO_sea']))
$sub_sql='and'.$cot." Like '".$sea."'";
else
$sub_sql='';
}
if($cot!='' && $cot=='n'){
$cot='`name`';
if(isset($_REQUEST['SO_sea2']))
$sub_sql='and'.$cot." Like '%".$sea2."%'";
else
$sub_sql='';
}
if($cot!='' && $cot=='a'){
$cot='`address`';
if(isset($_REQUEST['SO_sea3']))
$sub_sql='and'.$cot." Like '%".$sea3."%'";
else
$sub_sql='';
}
//end sap xep
##############################################################

include("../JO_02_function.php");
include("../JO_01_configuration.php");
$JO_MySQL = new mysqli( $JO_DB_host, $JO_DB_user, $JO_DB_password, $JO_DB_database );
$JO_MySQL -> query( "SET NAMES 'UTF8'" );

if($kieu!='')
{   
	if($kieu=="DESC")  
	{
?>

<table>

					<th><span id="id"   onclick="sorting('ASC','id','<?=isset($_REQUEST['Page'])?$_REQUEST['Page']:"1"?>')" style="cursor:pointer">key		</span></th>
                    <th><span id="name" onclick="sorting('ASC','n','<?=isset($_REQUEST['Page'])?$_REQUEST['Page']:"1"?>')" style="cursor:pointer">name	</span></th>
                    <th><span id="add"  onclick="sorting('ASC','a','<?=isset($_REQUEST['Page'])?$_REQUEST['Page']:"1"?>')" style="cursor:pointer">address	</span></th>
                    <th></th>
                     <tr>
                         <td><input type="text" id="txt_id"    value="<?=$sea?>" 	name="txt_id"		 style="width:20px !important" onkeypress="searchKeyPress(event,,'id')" onclick="xoa()"/></td>
                        <td><input type="text" id="txt_ten"    value="<?=$sea2?>"   	name="txt_ten"		 style="width:212px !important" onkeypress="searchKeyPress(event,'n')" onclick="xoa2()"/></td>
                        <td><input type="text" id="txt_diachi" value="<?=$sea3?>"  name="txt_diachi"	 style="width:212px !important"  onkeypress="searchKeyPress(event,'a')" onclick="xoa3 ()"/></td>
                        <td class="btnadd"><span style="cursor:pointer">Add</span>	
                    </tr>
                <!--load data-->
               
				   <?php 
				   $querycount="
			SELECT * FROM organization WHERE `delete` is NULL  ".$sub_sql."
";
            $recordSetnum = JO_query( $querycount );
			$total=$recordSetnum->num_rows;
			
			$page=1;
			$limit=10;
			if(($total%$limit)!=0) {$numpages=(int)($total/$limit)+1;}
			else                   {$numpages=$total/$limit;}
			if(isset ($_REQUEST["Page"])){$page=$_REQUEST["Page"];}
			$vt=(($page-1)*$limit);
			   
                        $query = "
                            SELECT	*
                            FROM	`organization`
                            WHERE	`organization`.`delete` is null ".$sub_sql." order by ".$cot." ASC Limit $vt,$limit
                        ";
						
                        $recordSet = JO_query( $query );
                        if( $recordSet -> num_rows ) 
                        while($record = $recordSet -> fetch_array( ))
						{
                     
                   ?>
					<tr>
                        <td><?=$record['key']												?></td>
                        <td><?=$record['name']												?></td>
                        <td><?=$record['address']==""?"Chưa Có Thông Tin":$record['address']?></td>
                        <td class="btnedit"><span style="cursor:pointer">Edit</span>		
                    </tr>
              
<?php  					}
?>
          <tr >
            <td style="text-align: center;border-bottom: 0px" colspan="3"><?

                if($numpages>1){
                    if($page>2){
                    ?>
              <a href="#" onclick="sorting('DESC','id','1');">◀</a>
              <?
                    }
                    if($page>1){
                    ?>
              <a href="#" onclick="sorting('DESC','id','<?=($page-1)?>');">◁</a>
              <?
                    }
                    if($page-4>1) echo "...";
                    for($i=$page-4;$i<=($page-1);$i++){
                        if($i>0){
                        ?>
              <a href="#" onclick="sorting('DESC','id','<?=$i?>');">
              <?=$i?>
              </a>
              <?
                        }
                    }
                    echo  "<b style='color=#ffffff;'>".$page."</b>";
                    for($i=$page+1;$i<=($page+4);$i++){
                        if($i<=$numpages){
                        ?>
              <a href="#" onclick="sorting('DESC','id','<?=$i?>');">
              <?=$i?>
              </a>
              <?
                        }
                    }
                    if($page+4<$numpages) echo "...";
                    if($page<$numpages){
                    ?>
              <a href="#" onclick="sorting('DESC','id','<?=($page+1) ?>');">▷</a>
              <?
                    }
                    if($page<($numpages-1)){
                    ?>
              <a href="#" onclick="sorting('DESC','id','<?=$numpages ?>');">▶</a>
              <?
                    }

                }
                 ?></td>
          </tr>
<?php
	}if($kieu=='ASC')
	{
		
?>
	<table>

					<th><span id="id"   onclick="sorting('DESC','id','<?=isset($_REQUEST['Page'])?$_REQUEST['Page']:"1"?>')" style="cursor:pointer">key		</span></th>
                    <th><span id="name" onclick="sorting('DESC','n','<?=isset($_REQUEST['Page'])?$_REQUEST['Page']:"1"?>')"   style="cursor:pointer">name	</span></th>
                    <th><span id="add"  onclick="sorting('DESC','a','<?=isset($_REQUEST['Page'])?$_REQUEST['Page']:"1"?>')"   style="cursor:pointer">address	</span></th>
                    <th></th>
                     <tr>
                         <td><input type="text" id="txt_id"    value="<?=$sea?>" 	name="txt_id"		 style="width:20px !important" onkeypress="searchKeyPress(event,'id')" onclick="xoa()"/></td>
                        <td><input type="text" id="txt_ten"    value="<?=$sea2?>"   	name="txt_ten"		 style="width:212px !important" onkeypress="searchKeyPress(event,'n')" onclick="xoa2()"/></td>
                        <td><input type="text" id="txt_diachi" value="<?=$sea3?>"  name="txt_diachi"	 style="width:212px !important"  onkeypress="searchKeyPress(event,'a')" onclick="xoa3 ()"/></td>
                        <td class="btnadd"><span style="cursor:pointer">Add</span>	
                    </tr>
                <!--load data-->
               
				   <?php  
				     $querycount="
			SELECT * FROM organization WHERE `delete` is NULL ".$sub_sql."
";
            $recordSetnum = JO_query( $querycount );
			$total=$recordSetnum->num_rows;
           $page=1;
			$limit=10;
			if(($total%$limit)!=0) {$numpages=(int)($total/$limit)+1;}
			else                   {$numpages=$total/$limit;}
			if(isset ($_REQUEST["Page"])){$page=$_REQUEST["Page"];}
			$vt=(($page-1)*$limit);
                        $query = "
						
                            SELECT	*
                            FROM	`organization`
                            WHERE	`organization`.`delete` is null  ".$sub_sql." order by ".$cot." DESC Limit $vt,$limit
                        ";
						
                        $recordSet = JO_query( $query );
                        if( $recordSet -> num_rows ) 
                        while($record = $recordSet -> fetch_array( ))
						{
                        
                   ?>
					<tr>
                        <td><?=$record['key']												?></td>
                        <td><?=$record['name']												?></td>
                        <td><?=$record['address']==""?"Chưa Có Thông Tin":$record['address']?></td>
                        <td class="btnedit"><span style="cursor:pointer">Edit</span>		
                    </tr>
<?php		
						}?>
          <tr >
            <td style="text-align: center;border-bottom: 0px" colspan="3"><?

                if($numpages>1){
                    if($page>2){
                    ?>
              <a href="#" onclick="sorting('ASC','id','1');">◀</a>
              <?
                    }
                    if($page>1){
                    ?>
              <a href="#" onclick="sorting('ASC','id','<?=($page-1)?>');">◁</a>
              <?
                    }
                    if($page-4>1) echo "...";
                    for($i=$page-4;$i<=($page-1);$i++){
                        if($i>0){
                        ?>
              <a href="#" onclick="sorting('ASC','id','<?=$i?>');">
              <?=$i?>
              </a>
              <?
                        }
                    }
                    echo  "<b style='color=#ffffff;'>".$page."</b>";
                    for($i=$page+1;$i<=($page+4);$i++){
                        if($i<=$numpages){
                        ?>
              <a href="#" onclick="sorting('ASC','id','<?=$i?>');">
              <?=$i?>
              </a>
              <?
                        }
                    }
                    if($page+4<$numpages) echo "...";
                    if($page<$numpages){
                    ?>
              <a href="#" onclick="sorting('ASC','id','<?=($page+1) ?>');">▷</a>
              <?
                    }
                    if($page<($numpages-1)){
                    ?>
              <a href="#" onclick="sorting('ASC','id','<?=$numpages ?>');">▶</a>
              <?
                    }

                }
                 ?></td>
          </tr>
<?php
	}
}

?>	
	

 <script>
function sorting(k,f,p){
		var sea=escape(_utf8_encode($('#txt_id').val()));
			
			var sea2=escape(_utf8_encode($('#txt_ten').val()));
			
			var sea3=escape(_utf8_encode($('#txt_diachi').val()));
		
	 if(sea!='')
     var url="JO_71_SO/SO_16_01_managing_organization.php?SO_sea="+sea+"&SO_cot=id&SO_kieu="+k+"&Page="+p;
	 if(sea2!='')
     var url="JO_71_SO/SO_16_01_managing_organization.php?SO_sea2="+sea2+"&SO_cot=n&SO_kieu="+k+"&Page="+p;
	 if(sea3!='')
     var url="JO_71_SO/SO_16_01_managing_organization.php?SO_sea3="+sea3+"&SO_cot=a&SO_kieu="+k+"&Page="+p;
	 if(sea=='' && sea2=='' &&sea3=='')
   	 var url="JO_71_SO/SO_16_01_managing_organization.php?SO_kieu="+k+"&SO_cot=id&Page="+p;
	 $("#load_contend").load(url,10000,function(){
					initbtnedit()
					initbtnadd()
				});
	}
	/*$("span").click(
		function(){
		alert('a');
			//sorting('ASC',$(this).attr("id"),'<?//=$_REQUEST['Page']?>')
		}
	);*/
	function move()
	{
		if($("#tmp").length){
			$("#tmp").remove();
		}	
	}
	function move2()
	{
		if($("#tbp").length){
			$("#tbp").remove();
		}	
	}
	function submitform(F)
		{
			
			document.forms[F].submit();
			
		}
		function xoa(){
			$('#txt_ten').val('');
			$('#txt_diachi').val('');
			}
		function xoa2(){
			$('#txt_id').val('');
			$('#txt_diachi').val('');
			}
		function xoa3(){
			document.getElementById('txt_id').value='';
			$('#txt_ten').val('');
			$('#txt_id').val('');
			}
	
	function show()
		{	
		if($("#extendcontent").is(":hidden")){
			$("#extendcontent").show();
		}else{
			$("#extendcontent").hide();
		}
		
		}
</script>